package com.dzt.androidframework.activity;

import android.app.Dialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.widget.PopupMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.dzt.androidframework.R;
import com.dzt.androidframework.utils.JToastKit;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

public class ActivityAlert extends ActivityTitle {

	@ViewInject(R.id.btn_notification)
	private Button btnNotification;
	private boolean isShowNotification = false;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_alert;
	}

	@Override
	protected void initParams() {
		setTitleView(R.string.menu_1);
	}

	@Override
	protected void initData(Bundle savedInstanceState) {

	}

	@Override
	protected String[] initPermissions() {
		return null;
	}

	@Override
	public void onBtnClicked(Dialog dialog, int position, Object tag) {
		JToastKit.getInstance().showMessage("click pos = " + position);
	}

	@Event(value = {R.id.btn_alert, R.id.btn_wait, R.id.btn_toast,
			R.id.btn_pop_window, R.id.btn_notification})
	private void onViewClick(View v) {
		switch (v.getId()) {
			case R.id.btn_alert:
				showAlertDialog("这是标题", "这是警告提示框的内容",
						new String[]{"确定", "取消"}, true, true, null);
				break;
			case R.id.btn_wait:
				showWaitDialog("等待中...", true, null);
				break;
			case R.id.btn_toast:
				Toast toast = Toast.makeText(context, "", Toast.LENGTH_LONG);
				LayoutInflater inflater = LayoutInflater.from(context);
				View view = inflater.inflate(R.layout.toast_view, null);
				TextView textView = (TextView) view.findViewById(R.id.toast_text);
				textView.setText("this is toast");
				toast.setView(view);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				break;
			case R.id.btn_notification:
				Intent intent = new Intent(ActivityAlert.this, PlaybackService.class);
				if (isShowNotification) {
					isShowNotification = false;
					btnNotification.setText("显示大图通知");
					stopService(intent);
					//unbindService(connection);
				} else {
					isShowNotification = true;
					btnNotification.setText("隐藏大图通知");
					startService(intent);
					//bindService(intent, connection, Context.BIND_AUTO_CREATE);
				}
				break;
			case R.id.btn_pop_window:
				PopupMenu popup = new PopupMenu(context, v);
				popup.inflate(R.menu.filter_tasks);
				popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
					public boolean onMenuItemClick(MenuItem item) {
						switch (item.getItemId()) {
							case R.id.active:
								JToastKit.getInstance().showMessage("active");
								break;
							case R.id.completed:
								JToastKit.getInstance().showMessage("completed");
								break;
							default:
								JToastKit.getInstance().showMessage("all");
								break;
						}
						return true;
					}
				});
				popup.show();
				break;
			default:
				break;
		}
	}
	private PlaybackService playbackService;
	private ServiceConnection connection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
			playbackService = ((PlaybackService.LocalBinder)iBinder).getService();
		}

		@Override
		public void onServiceDisconnected(ComponentName componentName) {

		}
	};
}
